package cn.i96688.scanclient.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import cn.i96688.scanclient.bean.PayLog;
import cn.i96688.scanclient.exception.PayException;



public final class PayUtil {
	
//	private static Logger log=Logger.getLogger(PayUtil.class);
	
	/**
	 * 从源json复制数据到目标json
	 * @param target  目标
	 * @param source  源
	 */
	public static void copyJson(JSONObject target,JSONObject source){
		if(target==null || source==null)
			return;
		for(Map.Entry<String, Object> entry:source.entrySet()){
			Object value=entry.getValue();
			if(value instanceof JSONObject){
				JSONObject tmp=new JSONObject();
				copyJson(tmp,(JSONObject)value);
				target.put(entry.getKey(), tmp);
			}else{
				target.put(entry.getKey(), value);
			}
		}
	}
	
	
	/**
	 * 获取日期格式
	 * @return
	 */
	public static SimpleDateFormat getDateFormat(){
		return new SimpleDateFormat("yyyy-MM-dd");
	}
	/**
	 * 获取日期时间格式
	 * @return
	 */
	public static SimpleDateFormat getDateTimeFormat(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * 获取日期格式的字符串
	 * @return
	 */
	public static String getDate(){
		return getDateFormat().format(new Date());
	}
	/**
	 * 获取日期时间格式的字符串
	 * @return
	 */
	public static String getDateTime(){
		return getDateTimeFormat().format(new Date());
	}

	/**
	 * 参数校验
	 * @param reqData
	 * @param key
	 */
	public static void validateParam(JSONObject reqData,String key){
		if(!reqData.containsKey(key))
			throw new PayException("请求参数："+key+"不存在").setName("validate.param");
	}
	
	
	//金额验证  
	public static boolean isNumber(String str){   
	     Pattern pattern=Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式  
	     Matcher match=pattern.matcher(str);   
	     if(match.matches()==false){   
	        return false;   
	     }else{   
	        return true;   
	     }   
	 }  
	
	public static String readFile(String path){
		FileInputStream fis = null;
		InputStreamReader isr = null;
		try {
			fis = new FileInputStream(path); //读取配置文件
			isr = new InputStreamReader(fis, "UTF-8");
			char[] buffer=new char[1024];  
	        int len=0;  
	        StringBuilder sb=new StringBuilder();
	        if((len=isr.read(buffer))!=-1){  
	        	sb.append(buffer, 0, len);
	        }  
	        return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}finally{
			try {
				if(fis!=null){
					fis.close();
				}
			} catch (Exception e2) {
			}
			try {
				if(isr!=null){
					isr.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	public static void writeFile(String filePath,String content){
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			fos=new FileOutputStream(filePath);
			osw=new OutputStreamWriter(fos, "UTF-8");
			osw.write(content);
			osw.flush();
		} catch (Exception e) {
			PayLog.getInstance().addLogs(e);
		}finally{
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e2) {
			}
			try {
				if(osw!=null){
					osw.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	
	@SuppressWarnings("resource")
	public static String getLocalMAC() {
        InetAddress address;
        String sMAC = null;
        try {
            address = InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            byte[] mac = ni.getHardwareAddress();
            Formatter formatter = new Formatter();
            for (int i = 0; i < mac.length; i++) {
                sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i],
                        (i < mac.length - 1) ? "-" : "").toString();
            }
        } catch (Exception e) {
        	PayLog.getInstance().addLogs(e);
        }
        PayLog.getInstance().addLog("获取本机MAC="+sMAC);
        return sMAC;
    }
	public static String getOrderNoTest() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		int code=new Random().nextInt(10000);
		return new StringBuilder().append(sdf.format(new Date())).append(code).toString();
	}
}
